package cn.zyq.demo.controller.file;


import cn.zyq.demo.beans.file.FileStorage;
import cn.zyq.demo.service.file.FileStorageService;
import cn.zyq.demo.system.common.BackParam;
import cn.zyq.demo.system.common.BaseController;
import cn.zyq.demo.system.cos.CosService;
import cn.zyq.demo.system.enums.core.ResponseCodeEnum;
import cn.zyq.demo.system.utils.FileInfo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 公共接口类
 * @Author pengdoudou
 * @Date 2023/4/20 14:43
 */
@Slf4j
@Api(tags = "文件接口类")
@RestController(value = "fileCommonController")
@RequestMapping(value = "/file/")
public class FileController extends BaseController {
    @Autowired
    private CosService cosService;

    @Autowired
    private FileStorageService fileStorageService;
    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "上传文件", response = FileInfo.class)
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("【请求开始】上传文件,请求参数,file:{}", file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        FileStorage storage = cosService.store(file.getInputStream(), file.getSize(), file.getContentType(),
                originalFilename);
        log.info("【请求结束】上传文件,响应结果:{}", JSONObject.toJSONString(storage));
        BackParam backParam = new BackParam();
        backParam.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        backParam.setCode(ResponseCodeEnum.SUCCESS.getCode());
        backParam.setData(storage);
        return backParam;
    }

    /**
     * 访问存储对象
     *
     * @param key
     *            存储对象key
     * @return
     */
    @ApiOperation(value = "访问文件")
    @GetMapping("/{key:.+}")
    public ResponseEntity<Resource> fetch(@PathVariable String key) {
        log.info("【请求开始】访问本地存储对象,请求参数,key:{}", key);

        FileStorage Storage = fileStorageService.findByKey(key);
        if (key == null) {
            return ResponseEntity.notFound().build();
        }
        if (key.contains("../")) {
            return ResponseEntity.badRequest().build();
        }
        String type = Storage.getType();
        MediaType mediaType = MediaType.parseMediaType(type);

        Resource file = cosService.loadAsResource(key);
        if (file == null) {
            return ResponseEntity.notFound().build();
        }
        log.info("【请求结束】访问存储对象,响应结果:{},{}","成功", file);
        return ResponseEntity.ok().contentType(mediaType).body(file);
    }

}
